package com.sky.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/10/22 0:37
 */
@Api(tags = "套餐相关接口")
@RestController
@RequestMapping(value = "/admin/setmeal")
@Slf4j
public class SetmealController {
    @Resource
    private SetmealService setMealService;

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询套餐")
    @GetMapping(value = "/{id}")
    public Result<SetmealVO> getById(@PathVariable(value = "id") Long id) {
        SetmealVO setmealVO = setMealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 增加套餐信息
     *
     * @return
     */
    @PostMapping
    @ApiOperation(value = "增加套餐")
    public Result saveSetMeal(@RequestBody SetmealDTO setmealDTO) {
        setMealService.saveSetMeal(setmealDTO);
        return Result.success();
    }


    /**
     * 分页查询套餐信息
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation(value = "分页查询套餐信息")
    @GetMapping(value = "/page")
    public Result<PageResult> pageQuerySetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 开始分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        // 分页查询
        List<SetmealVO> setmealVO = setMealService.pageQuery(setmealPageQueryDTO);
        PageInfo<SetmealVO> setmealVOPageInfo = new PageInfo<>(setmealVO);
        PageResult pageResult = new PageResult(setmealVOPageInfo.getTotal(), setmealVOPageInfo.getList());
        return Result.success(pageResult);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @ApiOperation(value = "修改套餐信息")
    @PutMapping
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("setmealDTO = {}", setmealDTO);
        setMealService.updateSetMeal(setmealDTO);
        return Result.success();
    }

    /**
     * 修改套餐售卖状态
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation(value = "修改套餐状态")
    @PostMapping(value = "/status/{status}")
    public Result updateStatus(@PathVariable(value = "status") Integer status, Long id) {
        setMealService.updateStatus(id, status);
        return Result.success();
    }

    /**
     * 批量/单个删除套餐
     *
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "批量/单个删除套餐")
    public Result deleteSetmeal(@RequestParam(value = "ids") List<Long> ids) {
        setMealService.deleteSetmeal(ids);
        return Result.success();
    }
}
